package com.tk.exam.service.impl;

import com.github.pagehelper.PageInfo;
import com.tk.exam.dao.StudentDao;
import com.tk.exam.domain.TkExamInfo;
import com.tk.exam.domain.TkExamInfoExample;
import com.tk.exam.domain.TkStudent;
import com.tk.exam.exception.ApiException;
import com.tk.exam.mapper.TkExamInfoExtMapper;
import com.tk.exam.mapper.TkStudentMapper;
import com.tk.exam.service.StudentService;
import com.tk.exam.utils.CreateFileUtil;
import com.tk.exam.utils.DESUtil;
import com.tk.exam.vo.StudentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author: xujg
 * @Description: TODO
 * @date: 2019-06-17 13:43
 */
@Service
@Transactional
@Slf4j
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private TkStudentMapper studentMapper;
    @Autowired
    private TkExamInfoExtMapper examInfoMapper;

    @Override
    public PageInfo<StudentVo> getListWithPage(StudentVo param) {
//        String studentName = param.getStudentName();
//        if(!StringUtils.isEmpty(studentName)){
//            boolean mobile = StringUtils.isNumeric(studentName);
//            if(mobile){
//                param.setPhoneNo(studentName);
//                param.setStudentName(null);
//            }
//        }

        PageInfo<StudentVo> pageInfo = studentDao.getListWithPage(param);
        return pageInfo;
    }

    @Override
    public int edit(StudentVo param) {
        int i =0;
        Integer id = param.getId();
        TkStudent s = studentMapper.selectByPrimaryKey(id);
        if(s==null){
            throw ApiException.of("该学生不存在");
        }
        s.setDepartment(param.getDepartment());
        s.setStudentName(param.getStudentName());
        s.setPhoneNo(param.getPhoneNo());
        s.setUpdateTime(new Date());
        i = studentMapper.updateByPrimaryKey(s);
        return i;
    }

    @Override
    public PageInfo<TkStudent> getListOfClassWithPage(String classId) {
        return studentDao.getListOfClassWithPage(classId);


    }

    @Override
    public int downLoadTemplate() {
        int i =0;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        HttpServletRequest request = attributes.getRequest();
        String agent=request.getHeader("User-Agent").toLowerCase();

        CreateFileUtil.downloadLocal("studentTemplate.xlsx",response,agent);
        i=1;
        return i;
    }


    @Override
    public int importStudent(List<List<String>> list,String classId) {
        int i = 0;
        for (int j = 1; j < list.size(); j++) {
            TkStudent s = new TkStudent();
            s.setStudentName(list.get(j).get(1));
            String phoneNum = list.get(j).get(2);
            s.setPhoneNo(phoneNum);
            s.setDepartment(list.get(j).get(3));
            s.setClassId(Integer.parseInt(classId));
            String subPhoneNum = list.get(j).get(2).substring(5);
            System.out.println(subPhoneNum);
            s.setPassword(DESUtil.getEncryptPassword(subPhoneNum));
            s.setUpdateTime(new Date());
            s.setCreateTime(new Date());
            try{
                i = studentMapper.insert(s);
            }catch (DuplicateKeyException ex){
                log.info("======考生重复,考生手机："+s.getPhoneNo()+" 考生班级id： "+s.getClassId());
            }
        }
        return i;
    }

    @Override
    public int deleteStudent(String[] studentIds) {
        int i = 0;
        for (String studentId : studentIds) {
            TkStudent s = studentMapper.selectByPrimaryKey(Integer.parseInt(studentId));
            if(s==null){
                throw ApiException.of("该考生不存在");
            }

            TkExamInfoExample examInfoExample = new TkExamInfoExample();
            examInfoExample.createCriteria().andClassIdEqualTo(s.getClassId());
            List<TkExamInfo> examInfos = examInfoMapper.selectByExample(examInfoExample);
            if(!CollectionUtils.isEmpty(examInfos)){
                throw ApiException.of("该项目已被引用过，不能被删除！");
            }

        }


        for (String studentId : studentIds) {
            studentMapper.deleteByPrimaryKey(Integer.parseInt(studentId));
        }
        i=1;
        return i;
    }


}
